<script setup lang="ts">
import CameraCapture from "@/views/PublicCom/cameraCapture.vue";
import Upload from "@/views/PublicCom/ImageFilesUpload.vue";
import {useDataPublicStore} from '@/stores/dataPublicStore.ts';
import {onMounted, ref} from "vue";
import router from "@/router/index.ts";
import {ElMessage} from "element-plus";


import {useRoute} from "vue-router";
import {useSaveOneMaterialOutAPI} from "@/apis/materialOut.js";
import {getProjectListAPI} from "@/apis/projectInfo.js";
import {useGetOneByIdAPI} from "@/apis/material.js";

const route=useRoute();

const dataPublicStore=useDataPublicStore();

const materialOutNumber=ref<number>(0);
const description=ref('');
interface materialOutType {
  createBy: string;
  lastUpdateBy: string;
  materialName: string;
  brand: string;
  description: string;
  number: number;
  pieces: string;
  model: string;
  typeName: string;
  location: string;
  layout: string;
  materialPhotoList: string;
  encode: string;
  createTime: number;
  updateTime: number;
  recipient: string;
  receive_project: string;
}


const materialOutObject=ref<materialOutType>({
  brand: '',
  createBy: '',
  lastUpdateBy: '',
  createTime: 0,
  updateTime: 0,
  description: '',
  encode: '',
  layout: '',
  location: '',
  materialName: '',
  materialPhotoList: '',
  number: 0,
  pieces: '',
  typeName: '',
  model:'',
  recipient:'',
  receive_project:'',
});

interface MaterialType {
  id:number;
  materialName:string;
  typeName:string;
  encode:string;
  brand:string;
  layout:string;
  pieces:string;
  number:number;
  stockTakeNumber:number;
  model:string;
  location:string;
 }


const projectList=ref([]);

const materialObject=ref<MaterialType>({
  id:0,
  materialName:'',
  typeName:'',
  encode:'',
  brand:'',
  layout:'',
  pieces:'',
  number:0,
  stockTakeNumber:0,
  model:'',
  location:'',
})

onMounted(async ()=>{

  if(route.params.id===null||route.params.id===undefined){
    await router.push("/home/material");
  }
  else {
    const res=   await  useGetOneByIdAPI(route.params.id);
    materialObject.value=res.data;
  }

  const res= await getProjectListAPI(1,1000)
  projectList.value=res.data


})

// 显示进度条配合函数
const format=(percentage:number)=>{
  return percentage===100?'100%':`${percentage}`
}

const disableSwitch=ref(false);
/*todo 是否使用表单还是用其他方式，待定*/




const  submitSaveOneTransfer=async ()=> {
  if (materialOutNumber.value <= 0) {
    ElMessage({
      type: "warning",
      message: '数量不能小于等于0！'
    })
    return;
  }

  const formData = new FormData();
  // 材料名称
  formData.append('materialName', materialObject.value.materialName);
  // 材料表单id
  formData.append("materialId", materialObject.value.id);
  // 材料品牌
  formData.append('brand', materialObject.value.brand);
  // 备注
  formData.append('description', description.value);
  // 出场数量
  formData.append('number', materialOutNumber.value);
  // 材料数量单位
  formData.append('pieces', materialObject.value.pieces);
  // 材料型号
  formData.append('model', materialObject.value.model);
  // 材料类型
  formData.append('typeName', materialObject.value.typeName);
  // 材料所在地
  formData.append("warehouse", materialObject.value.location);
  // 货架号
  formData.append("layout", materialObject.value.layout);
  // 领取人

  if (materialOutObject.value.recipient === '' || materialOutObject.value.recipient === undefined) {
    materialOutObject.value.recipient = ''
  }
  formData.append("recipient", materialOutObject.value.recipient);
  // 领取项目
  formData.append("receiveProject", materialOutObject.value.receive_project);


  if (dataPublicStore.photoFileList.length > 0) {
    dataPublicStore.photoFileList.forEach((item: File) => {
      formData.append('photoFiles', item);
    })
  }

  if (dataPublicStore.uploadFileList.length > 0) {
    dataPublicStore.uploadFileList.forEach((item: File) => {
      formData.append('photoFiles', item);
    })
  }



  await useSaveOneMaterialOutAPI(formData)
  disableSwitch.value = true;
  dataPublicStore.setPhotoFiles([]);
  dataPublicStore.setUploadFileList([]);
  dataPublicStore.setOnUploadProgress(0);
  dataPublicStore.saveDataObject({});
  await router.push({path: '/home/material/'})
}


</script>

<template>
  <!--  进度条-->
  <el-progress :percentage="dataPublicStore.onUploadProgress" :format="format"    :stroke-width="5" v-show="dataPublicStore.onUploadProgress" style="margin: 0;padding: 0" ></el-progress>
  <!-- 面包屑 -->
  <div class="bread-container">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: `/` }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:`/home/material/`}" >材料列表</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:`/home/material/materialOutList`}" >材料出场列表</el-breadcrumb-item>
      <el-breadcrumb-item  >材料出场表单</el-breadcrumb-item>
    </el-breadcrumb>
  </div>


  <div class="divTable">
    <table class="table">
      <tbody>
      <tr>
        <td class="tableTitle" colspan="4" >材    料    出     场    表    单</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF;color: #242424;font-family: 微软雅黑,serif">材料名称</td>
        <td style="width: 250px;height: 15px;   text-align: center  ">{{ materialObject.materialName}}</td>
        <td style="width: 250px;height: 15px; text-align: center ;background: #EFF6FF"  >材料品牌</td>
        <td style="width: 350px;height: 15px;   text-align: center  ">{{ materialObject.brand}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">材料类型</td>
        <td style="width: 250px;height: 15px;text-align: center;">{{ materialObject.typeName}}</td>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">材料型号</td>
        <td style="width: 250px;height: 15px;text-align: center;">{{ materialObject.model}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">材料数量单位</td>
        <td style="width: 250px;height: 15px;text-align: center;">{{ materialObject.pieces}}</td>
        <!--        todo 这里要修改，记住-->
        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >原库存数量</td>
        <td style="width: 350px;height: 15px;text-align: center   ">
          {{materialObject.number}}
        </td>
      </tr>
      <tr>
        <td   style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >材料所在仓库</td>
        <td  style="width: 250px;height: 15px;text-align: center   ">
          {{ materialObject.location}}
        </td>
        <td   style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >材料出场数量</td>
        <td  style="width: 250px;height: 15px;text-align: center   ">
          <el-input  v-model="materialOutNumber" />
        </td>
      </tr>
      <tr>
        <td  style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">领取项目</td>
        <td style="margin: 0;padding: 0">
          <el-select
              clearable
              filterable
              allow-create
              default-first-option
              :reserve-keyword="false"
              v-model="materialOutObject.receive_project"
              placeholder="请添加或者选择"
              size="large"
              style="margin: 0;padding: 0"
          >
            <el-option
                v-for="item in projectList"
                :key="item.id"
                :label="item.name"
                :value="item.name"
            />
          </el-select>
        </td>
        <td   style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >材料领取人</td>
        <td  style="width: 250px;height: 15px;text-align: center   ">
          <el-input  v-model="materialOutObject.recipient" />
        </td>
      </tr>


      <tr>
        <td >
          备注
        </td>

        <td colspan="3">
          <el-input  v-model="description" />
        </td>
      </tr>


      <tr>
        <td colspan="4" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF"></td>
      </tr>
      <tr>
        <td colspan="4">
          <camera-capture/>
        </td>
      </tr>
      <tr>
        <td colspan="4">
          <upload/>
        </td>
      </tr>
      <tr>
        <td colspan="4" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">
          <el-button
              type="success"
              size="large"
              @click="submitSaveOneTransfer"
          >
            确定
          </el-button>
        </td>
      </tr>
      </tbody>
    </table>
  </div>

</template>

<style scoped lang="scss">
.bread-container {
  padding: 25px 10px;
  margin-left: 10px;
}
.divTable {
  position: absolute;
  top: 150px;
  bottom: 0;
  left: 300px;
  right: 0;
  margin: auto;
}


table, td,tr{
  border: 1px solid #44a8da;
  text-align: center;
  border-spacing: 0;
  border-collapse: collapse;
  height: 45px;
}

.tableTitle{

  border-right: 0 none;
  border-left: 0 none;
  text-align: center;
  font-size: large;
  font-weight: bold;
}

.demo-image__error .image-slot {
  font-size: 30px;
}
.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}
.demo-image__error .el-image {
  width: 100%;
  height: 200px;
}

</style>